Page({
  data: {
    order_id: '',
    order_source : '',
    supermarket_id: '',
    order_products: [],
    hasChild: false,
    amount_detail : [],
    takeup : false,
    operations : [],
    od : null ,
    invoice_type : 0,
    isShow : false,
  },

  onLoad: function (options) {
    this.setData({
      order_id: options.order_id,
      order_source: options.order_source,
      supermarket_id: options.supermarket_id
    })
    
  },

  onShow : function(){
    this.setData({
      isShow: false,
    })
    this.getMyOrderInfo();
  },

  getMyOrderInfo() {
    var url = getApp().data.url + 'order/getMyOrderInfo';
    var body = {
      "order_id": this.data.order_id, "order_source": this.data.order_source,
      "supermarket_id": this.data.supermarket_id
    };
    
    wx.showLoading({
      title: '加载中..',
    })
    getApp().wxAjax(this.splicegetMyOrderInfo, url, body);
  },

  //查询回调
  splicegetMyOrderInfo(res) {
    var d = res.data.body;
    console.log(d)
    var order_products = d.order_products;
    if (order_products.length > 2){
      this.setData({
        hasChild : true
      })
    }else{
      this.setData({
        hasChild: false
      })
    }
    var arr = [];
    for (var i=0; i < order_products.length;i++){
      if(i<2){
        arr.push(true);
      }else{
        arr.push(false);
      }
      order_products[i].isopen = arr[i];
    }

    var amount_detail = d.amount_detail;
    var arr = [];
    for (var i = 0; i < amount_detail.length; i++) {
      if (amount_detail[i].value.indexOf("-")==0) {
        arr.push("detailValeCo");
      } else {
        arr.push("");
      }
      amount_detail[i].iso = arr[i];
    }

    var operations = d.operations;
    if (operations == undefined){
      operations = [];
    }
    var invoice_type = d.invoice_type;
    if (invoice_type == undefined) {
      invoice_type = 0;
    }
    
    this.setData({
      od: d,
      order_products: order_products,
      amount_detail: amount_detail,
      operations: operations,
      invoice_type: invoice_type,
      isShow: true
    })

    wx.hideLoading();
  },
  
  openChild(){
    var order_products = this.data.order_products;
    for (var i = 0; i < order_products.length;i++){
      order_products[i].isopen = true;
    }
    this.setData({
      order_products: order_products,
      hasChild : false,
      takeup : true
    })
  },

  takeupChild(){
    var order_products = this.data.order_products;
    for (var i = 0; i < order_products.length; i++) {
      if(i<2){
        order_products[i].isopen = true;
      }else{
        order_products[i].isopen = false;
      }
    }
    this.setData({
      order_products: order_products,
      hasChild: true,
      takeup: false
    })
  },

  //取消订单
  cancelorder(e) {
    var me = this;
    wx.showModal({
      content: '确定取消订单？',
      cancelText: "暂不取消",
      cancelColor: "#999999",
      confirmColor: "#FF4844",
      success: function (res) {
        if (res.confirm) {
          var orderid = e.currentTarget.dataset.orderid;
          var url = getApp().data.url + 'order/cancelorder';
          var body = {
            "order_id": orderid
          };
          wx.showLoading({
            title: '加载中..',
          })
          getApp().wxAjax(me.splicecancelorder, url, body);
          wx.hideLoading();
        } else if (res.cancel) {

        }
      }
    })
  },
  //取消订单回调
  splicecancelorder(res) {
    var me = this;
    var resObj = res.data.header;
    console.log(resObj)
    var res_code = resObj.res_code;
    if (res_code == 0) {
      wx.showToast({
        title: '取消成功',
        icon: 'success',
        duration: 1000,
        success: function (res) {
          setTimeout(function ()
          { me.getMyOrderInfo() }, 1000);
          wx.setStorageSync("myDetailsUp", "y");
        }
      });
    } else {
      wx.showToast({
        title: '取消失败！请联系管理员！',
        icon: 'none',
        duration: 1000
      })
    }
  },
  //确认收货
  confirmorder(e) {
    var orderid = e.currentTarget.dataset.orderid;
    var url = getApp().data.url + 'order/confirmorder';
    var body = {
      "order_id": orderid
    };
    wx.showLoading({
      title: '加载中..',
    })
    getApp().wxAjax(this.spliceconfirmorder, url, body);
    wx.hideLoading();
  },
  //确认收货回调
  spliceconfirmorder(res) {
    var me = this;
    var resObj = res.data.header;
    console.log(resObj)
    var res_code = resObj.res_code;
    if (res_code == 0) {
      wx.showToast({
        title: '确认收货成功',
        icon: 'success',
        duration: 1000,
        success: function (res) {
          setTimeout(function ()
          { me.getMyOrderInfo() }, 1000);
          wx.setStorageSync("myDetailsUp", "y");
        }
      });
    } else {
      wx.showToast({
        title: '确认收货失败！请联系管理员！',
        icon: 'none',
        duration: 1000
      })
    }
  },
  //催单
  reminderOrder(e) {
    var orderid = e.currentTarget.dataset.orderid;
    var url = getApp().data.url + 'order/reminderOrder';
    var body = {
      "order_id": orderid
    };
    wx.showLoading({
      title: '加载中..',
    })
    getApp().wxAjax(this.splicereminderOrder, url, body);
    wx.hideLoading();
  },
  //催单回调
  splicereminderOrder(res) {
    var me = this;
    var resObj = res.data.header;
    console.log(resObj)
    var res_code = resObj.res_code;
    if (res_code == 0) {
      wx.showToast({
        title: resObj.message,
        icon: 'success',
        duration: 1000,
        success: function (res) {
          setTimeout(function ()
          { me.getMyOrderInfo() }, 1000);
        }
      });
    } else {
      wx.showToast({
        title: '催单失败！请联系管理员！',
        icon: 'none',
        duration: 1000
      })
    }
  },
  //拨打电话
  contactUs: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.showActionSheet({
      itemList: [phone],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx.makePhoneCall({
            phoneNumber: phone,
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  toPayset(e) {
    var order_id = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '../payset/payset?order_id=' + order_id
    })
  },
})